<?php
include_once("core/xmlxslt.php");

/**
 * VersionHandler
 * Class that provide functions to handle packages and versions. 
 *
 * @copyright inovia
 * @package oolime_core   
 */
class VersionHandler{
  public  $versionPathList;
  private $numberList;
  public  $name;
  public  $response;
  

  /**
   * Constructor  
   * get all the version of the packages available on the server.
   * @param : string the path pattern in regexp to search.
   */           
  function __construct($string){
    global $response;
    $this->response = &$response;
    
    $i=0;
    if($string=="")  $string="resources/packages/framework/version*";
    
    $this->name=$string;
    
    foreach (glob($string) as $filename) {
       $this->versionPathList[$i] = $filename;
       $temp = explode("_",$filename);
       $this->numberList[$i]=$temp[1];
       $i++;
    }
  }

/**
 * Returns all the versions info in a DomElement (works for versions framework)
 * @return DOMNode XMl node containing all the versions nodes
 */    
  function getVersionList(){
    $root = new DOMElement('versions');
    $this->response->doc->appendChild($root);
    $root->setAttribute("name",$this->name);
    if(count($this->versionPathList)){
      foreach ($this->versionPathList as $versionPath){
      	if (is_file($versionPath)) continue;
        $versionO     = $this->getVersionByPath($versionPath);
        $versionMod   = explode("/",$versionPath);
        $versionName  = $versionMod[count($versionMod)-1];
        $t            = $versionO->exportXML($this->response->doc);            
        $root->appendChild($t);
      }
    }
    return $root;
  }
  
  /**
   * same as getVersionList but for modules.
   * @return DOMNode a node containing all the modules
   */        
  function getModList(){
    $root = new DOMElement('modules');
    $this->response->doc->appendChild($root);
    $root->setAttribute("name",$this->name);
    if(count($this->versionPathList)){
      foreach ($this->versionPathList as $versionPath){
        $versionO     = new Version($versionPath."/info.xml");
        $versionMod   = explode("/",$versionPath);
        $versionName  = $versionMod[count($versionMod)-1];
        $t            = $versionO->exportXML($this->response->doc,"module");            
        $root->appendChild($t);
      }
    }
    return $root;
  }
  
  /**
   *  return the version currently use.
   *  @return string the version working   
   */   
  function getMyVersion(){
    $root = new DOMElement('MyVersion');
    $this->response->doc->appendChild($root);
    try{
      $versionO = new Version("");
      $t        = $versionO->exportXML($this->response->doc);            
      $root->appendChild($t);
    }catch(Exception $e){
      $this->response->addError4("Unable to locate info.xml");
    }
    return $root;
  }
  
  /**
   * Returns the last existing version
   * @return string last version
   */        
  function getMoreRecent(){
    return max($this->numberList);
  }
  
  /**
   *  return the version with the number $num
   *  @param string version number to return
   *  @return Version the version of this number   
   */      
  function getVersionByNumber($num){
    $i=0;
    foreach($this->numberList as $number){
      if ($number==$num){
        $temp=new Version($this->versionPathList[$i]."/info.xml");
        return $temp;
      }
      $i++;
    }
  }
  
  /**
   *  return the version named string
   *  @param the path of the version
   *  @return Version the version located in this path      
   */   
  function getVersionByPath($string){
    return new Version($string."/info.xml");
  }
  
  /**
   *  return the moduleByName
   *  @param string the surname of the version
   *  @return the Version of this surname   
   */      
  function getModByName($string){
    foreach($this->versionPathList as $path){
    $v=new Version($path."/info.xml");
      if ($v->getSurname()==$string){
        return $v;
      }
    }
  }
}


/**
 * Object version. That's a wrapper for the xml info file.
 * @author Baptiste Manson, Eric Raffin 
 * @package oolime_core
 */  
class Version{
  public $rootDomNode;
  public $path;
  /**
   *Construct an object to access the version file at the string path (the path is the info.xml path).
   */ 
  function __construct($string){
    $temp= new DOMDocument();
    if($string=="")  $string="info.xml";
    if(file_exists($string)){
      $temp->load($string);
      $this->rootDomNode=$temp->documentElement;
      
      //get the path of the plug in
      $strTemp=explode("/",$string);
      $mar="";
      for($i=0;$i<count($strTemp)-1;$i++){
        $this->path.=$mar.$strTemp[$i];
        $mar="/";
      }
    }else{
      throw new Exception("the file doesn't exist");
    }
  }
  
  /**
   * Returns the version number of the Version
   * @return string version number   
   */     
  function getNumber(){
    $temp= $this->rootDomNode->getElementsByTagName("version")->item(0);
    return $temp->nodeValue;
  }
  
  /**
   * Returns the surname of the version
   * @return string surname of the version   
   */     
  function getSurname(){
    $temp= $this->rootDomNode->getElementsByTagName("surname")->item(0);
    return $temp->nodeValue;
  }
  
  /**
   * Returns the comment associated with the version
   * @return string comment associated to the version
   */        
  function getComments(){
    $temp= $this->rootDomNode->getElementsByTagName("comments")->item(0);
    return $temp->nodeValue;
  }
  
  /**
   * Returns the date of the version
   * @return string date of the version   
   */     
  function getDate(){
    $temp= $this->rootDomNode->getElementsByTagName("date")->item(0);
    return $temp->nodeValue;
  }
  
  /**
   * Returns an array of directories containing the release
   * @return string array the directories of version files
   */     
  function getcontainsD(){
    $temp= $this->rootDomNode->getElementsByTagName("containsD");
    $i=0;
    foreach($temp as $directory){
      $tab[$i]= $directory->nodeValue;
      $i++;
    }
    return $tab;
  }
  
  /**
   * Returns an array of files containing the release
   * @return string array the files of version 
   */     
  function getcontainsF(){
    $temp= $this->rootDomNode->getElementsByTagName("containsF");
    $i=0;
    foreach($temp as $file){
      $tab[$i]= $file->nodeValue;
      $i++;
    }
    return $tab;
  }
  
  /**
   *  returns a Version DomNode containing all the informations of the version
   *  @return DOMNode the XML node containing version informations   
   */   
  function exportXML($doc,$kind = "version"){
    $versionValue = $doc->createElement("version");
    //add the name
    $versionNameNode    = $doc->createElement("name");
  	$versionNameMessage = $doc->createTextNode($this->getSurname());
  	$versionNameNode->appendChild($versionNameMessage);
  	$versionValue->appendChild($versionNameNode);
  	
  	//add the number of the version
  	$versionNumNode    = $doc->createElement("number");
  	$versionNumMessage = $doc->createTextNode($this->getNumber());
  	$versionNumNode->appendChild($versionNumMessage);
  	$versionValue->appendChild($versionNumNode);
  	
  	//add the comments of the version
  	$versionComNode    = $doc->createElement("comments");
  	$versionComMessage = $doc->createTextNode($this->getComments());
  	$versionComNode->appendChild($versionComMessage);
  	$versionValue->appendChild($versionComNode);
  	
  	//add the url for install of the framework
  	$versionURLNode    = $doc->createElement("URL");
  	if($kind == "version"){
  	  $versionURLMessage = $doc->createTextNode("index.php?action=updateVersion&number=".$this->getNumber());
  	}else if($kind == "module"){
  	  $versionURLMessage = $doc->createTextNode("index.php?action=installModule&number=".$this->getNumber()."&surname=".$this->getSurname());
    }
  	$versionURLNode->appendChild($versionURLMessage);
  	$versionValue->appendChild($versionURLNode);
  	
  	//add the url for install of the modules
  	$versionURLNode    = $doc->createElement("URL2");
  	$versionURLMessage = $doc->createTextNode("index.php?action=installModule&name=".$this->getSurname());
  	$versionURLNode->appendChild($versionURLMessage);
  	$versionValue->appendChild($versionURLNode);
  	
  	return $versionValue;
  }
}
?>
